package com.zmn.oms.dubbo.impl.zmn.master.order;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.business.interfaces.completecode.CompleteCodeBService;
import com.zmn.oms.dubbo.dto.common.work.BaseRequestDIO;
import com.zmn.oms.dubbo.dto.common.work.MasterWorkCompleteCodeDIO;
import com.zmn.oms.dubbo.interfaces.zmn.master.order.MasterWorkCompleteCodeRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.completecode.CompleteCodeDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 类描述：完成码
 *
 * @author liuying
 * @date 2021/07/20 17:36
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class MasterWorkCompleteCodeRemoteServiceImpl implements MasterWorkCompleteCodeRemoteService {

    @Resource
    private CompleteCodeBService completeCodeBService;
    @Resource
    private OrderWorkService orderWorkService;

    @Override
    public ResponseDTO getCompleteCode(@NotNull(message = "入参不能为空") BaseRequestDIO requestDIO) {
        if (Objects.isNull(requestDIO.getWorkId())) {
            return ResponseDTO.fail("工单不存在");
        }

        String completeCode = completeCodeBService.getCompleteCode(requestDIO.getWorkId());
        return ResponseDTO.success(completeCode);
    }

    @Override
    public ResponseDTO saveCompleteCode(@NotNull(message = "入参不能为空") MasterWorkCompleteCodeDIO masterWorkCompleteCodeDIO) {
        log.info("MasterWorkCompleteCodeRemoteService#saveCompleteCode req【{}】", masterWorkCompleteCodeDIO);

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(masterWorkCompleteCodeDIO.getOrderId(), masterWorkCompleteCodeDIO.getOrderId());
        if (Objects.isNull(orderWork)) {
            return ResponseDTO.fail("工单不存在");
        }

        CompleteCodeDTO completeCodeDTO = new CompleteCodeDTO();
        completeCodeDTO.setOrderId(masterWorkCompleteCodeDIO.getOrderId());
        completeCodeDTO.setWorkId(masterWorkCompleteCodeDIO.getWorkId());
        completeCodeDTO.setOperatorId((long) masterWorkCompleteCodeDIO.getMasterId());
        completeCodeDTO.setOperator(orderWork.getMasterName());
        completeCodeDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
        completeCodeDTO.setCompleteCode(masterWorkCompleteCodeDIO.getCode());
        completeCodeBService.addCompleteCode(completeCodeDTO);

        return ResponseDTO.success();
    }
}
